package com.zm.zyz.util;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.logging.log4j.Log4jImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class MyBatisHelper {
    private static SqlSessionFactory sqlSessionFactory;
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    private static String mapperPackage;


    static{
        try {
            Properties properties = new Properties();
            InputStream resourceAsStream = MyBatisHelper.class.getClassLoader().getResourceAsStream("db.properties");
            properties.load(resourceAsStream);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            mapperPackage = properties.getProperty("mapperPackage");

            configMyBatisPlus();
        } catch (IOException e) {
            throw new RuntimeException("MyBatisPlus初始化失败",e);
        }
    }

    public static void configMyBatisPlus() throws IOException {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        MybatisConfiguration configuration = new MybatisConfiguration();
        // 驼峰命名
        configuration.setMapUnderscoreToCamelCase(true);
        // 开启返回主键值功能
        configuration.setUseGeneratedKeys(true);
        // 添加拦截器插件
        //创建mybatis-plus插件对象
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //构建分页插件
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true);
        paginationInnerInterceptor.setMaxLimit(500L);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        configuration.addInterceptor(interceptor);
        //扫描mapper接口所在包
        configuration.addMappers(mapperPackage);
        //配置日志实现
        configuration.setLogImpl(Log4jImpl.class);
        //构建mybatis-plus需要的globalconfig
        GlobalConfig globalConfig = new GlobalConfig();
        //此参数会自动生成实现baseMapper的基础方法映射
        globalConfig.setSqlInjector(new DefaultSqlInjector());
        //设置id生成器
        globalConfig.setIdentifierGenerator(new DefaultIdentifierGenerator());
        //设置超类mapper
        globalConfig.setSuperMapperClass(BaseMapper.class);
        //给configuration注入GlobalConfig里面的配置
        GlobalConfigUtils.setGlobalConfig(configuration, globalConfig);
        //设置数据源
        Environment environment = new Environment("dev",
                new JdbcTransactionFactory(),
                new PooledDataSource(driver,
                        url,
                        username,
                        password));
        configuration.setEnvironment(environment);
        //构建sqlSessionFactory
         sqlSessionFactory = builder.build(configuration);
//        this.session = sqlSessionFactory.openSession();
    }

    public static SqlSession getSession(){

        return sqlSessionFactory.openSession();
    }
}
